package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import utility.Record;

import business.AnneeEtudeBL;
import business.EnseignantBL;
import business.TypeHeureBL;

public class PanelHeureUI extends AbstractParamPane implements ActionListener {
	
	private TypeHeureBL typeHeureBL;
    private JButton ajouter, modifier, supprimer;
    private JList list;
	
	public PanelHeureUI() {
	
		//Create and populate the panel.
        setLayout(new BorderLayout());
		setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createTitledBorder("Type d'heure"),
				BorderFactory.createEmptyBorder(5,5,5,5)));

		typeHeureBL = new TypeHeureBL();
		list = new JList();
		refreshListe();
        
        ajouter = new JButton ("Ajouter");
        modifier= new JButton ("Modifier");
        supprimer = new JButton ("Supprimer");
        
        ajouter.addActionListener(this);
        modifier.addActionListener(this);
        supprimer.addActionListener(this);

        JPanel commandBox = new JPanel();
        commandBox.add(ajouter);
        commandBox.add(modifier);
        commandBox.add(supprimer);
        
        drawCommandBox(commandBox);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == ajouter) {
			PanelHeureUIConsulter panel = new PanelHeureUIConsulter(AbstractViewPane.ViewMode.AJOUT, -1, this);
			panel.setVisible(true);
			refreshListe();
		}
		if (e.getSource() == modifier) {
			if (list.getSelectedIndex() != -1) {
				PanelHeureUIConsulter panel = new PanelHeureUIConsulter(AbstractViewPane.ViewMode.MODIFICATION, this.getIdSelectedItem(), this);
				refreshListe();
				panel.setVisible(true);
			}
		}
		if (e.getSource() == supprimer) {
			if (list.getSelectedIndex() != -1) {
				loadSupprimerViewPane();
			}
		}	
	}
	
	public int getIdSelectedItem(){
		return ((Record)list.getSelectedValue()).getId();
	}
	
	/**
	 * Permet la suppression d'un type d'heure
	 */
	public void heureUISupprimer(){

		int retour = JOptionPane.showConfirmDialog(
			    this,
			    "Êtes vous sûr de vouloir supprimer le type d'heure\n"+
			    //typeHeureBL.getNom()
			    " ?",
			    "Avertissement",
			    JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
		if (retour == JOptionPane.YES_OPTION) {
			TypeHeureBL typeASuppr = new TypeHeureBL(this.getIdSelectedItem());
			typeASuppr.supprimerTypeHeure();
		}
	}
	
	/**
	 * 
	 */
	public void refreshListe() {
		Vector<Record> listeRecord = new Vector<Record>();
		for(Record recordType : typeHeureBL.getListeTypeHeure()) {
			listeRecord.add(recordType);
		}
		list.setListData(listeRecord);
        add(list);
	}
	
	public void loadSupprimerViewPane() {
		heureUISupprimer();
		refreshListe();
	}
	
}
